<?
	$title="Add Product";
	
	require("required/dbconnect.php");
	require_once("required/function.php");
	checkAccess("allow_product_add",1);
	
	$msg="";
	$maxWidth_L=650;
	$maxHeight_L=650;
	
	$maxWidth_S=250;
	$maxHeight_S=250;
	
	if(isset($_POST['btnsubmit']))
	{
		$product_code=$_POST['product_code'];
		$product_name_eng=$_POST['product_name_eng'];
		
		$chk_pcode=mysql_query("select count(*) as result from os_products where product_code='$product_code'")or die(mysql_error());
		$chk_pname=mysql_query("select count(*) as result from os_products where product_name_eng='$product_name_eng'")or die(mysql_error());
			
		$msg="";
		
		
		if(mysql_result($chk_pcode,0,"result")>0)
		{	
			$msg.="Product code already exist, please enter unique product code<br>";
		}
		
		if(mysql_result($chk_pname,0,"result")>0)
		{	
			$msg.="Product name already exist, please enter unique product name<br>";
		}
		
		
		if($msg=="")
		{
			$allowed_ext = "jpg, gif, jpeg, JPEG, JPG, GIF"; 
			$max_size = "2000000"; 
			$pic_flag=-2;
			$width=0;
			$height=0;
			
			
			if(is_uploaded_file($_FILES['file']['tmp_name'])==true)
			{
				
				$extension = pathinfo($_FILES['file']['name']);
				$extension = $extension['extension'];
				$allowed_paths = explode(", ", $allowed_ext);
				
				for($i = 0; $i < count($allowed_paths); $i++)
				{
					if ($allowed_paths[$i] == "$extension")
					{
						$pic_flag = 1;
					}
				}
			
				// Check File Size
				if ($pic_flag == 1) 
				{
					
					if(is_uploaded_file($_FILES['file']['tmp_name']))
					{
						$uploadedfile = $_FILES['file']['tmp_name'];
						$tempfile=getimagesize($uploadedfile);
						$width=$tempfile[0];
						$height=$tempfile[1];
						
						
						if($extension=="jpg" || $extension=="jpeg" || $extension=="JPG" || $extension=="JPEG")
						{	
							$src = imagecreatefromjpeg($uploadedfile);
						}
						elseif($extension=="gif" || $extension=="GIF")
						{
							$src = imagecreatefromgif($uploadedfile);
						}
						elseif($extension=="png" || $extension=="PNG")
						{
							$src = imagecreatefrompng($uploadedfile);
						}
						else
						{
							$pic_flag=-2;//invalid extension
						}
					
					
						//==== insert thumbnail =============
						if($width>$height)
						{
			
							if($width>$maxWidth_S)
							{
								$newwidth=$maxWidth_S;
								$newheight=($height/$width)*$maxWidth_S;
							}
							else
							{
								$newwidth=$width;
								$newheight=$height;
							}
						}
						else
						{
			
							if($height>$maxHeight_S)
							{
								$newheight=$maxHeight_S;
								$newwidth=($width/$height)*$maxHeight_S;
							}
							else
							{
								$newheight=$height;
								$newwidth=$width;
							}
						}
						$tmp_s=imagecreatetruecolor($newwidth,$newheight);
						imagecopyresampled($tmp_s,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
						
						//====================================
						
						//==== insert large =============
						if($width>$height)
						{
							if($width>$maxWidth_L)
							{
								$newlwidth=$maxWidth_L;
								$newlheight=($height/$width)*$maxWidth_L;
							}
							else
							{
								$newlwidth=$width;
								$newlheight=$height;
							}
			
						}
						else
						{
							if($height>$maxHeight_L)
							{
								$newlheight=$maxHeight_L;
								$newlwidth=($width/$height)*$maxHeight_L;
			
							}
							else
							{
								$newlheight=$height;
								$newlwidth=$width;
							}
						}
						$tmp_l=imagecreatetruecolor($newlwidth,$newlheight);
						
						imagecopyresampled($tmp_l,$src,0,0,0,0,$newlwidth,$newlheight,$width,$height); 
						
						
					}
						
				}
				else 
				{
					$pic_flag=-2;
				}
			
			}else{
				$pic_flag=0;
			}
			
			if($pic_flag!=0 && $pic_flag!=1)
			{
				switch($pic_flag)
				{
					case(-2):
						$msg="Invalid picture extension, must be .jpg, .gif or .png";
						break;
				}		
			}
			else
			{
				$display_name=$_POST['display_name'];
				$totalitem=$_POST["totalitem"];
				$cat_id=$_POST['cat_id'];
				$sub_cat=$_POST['sub_cat'];
				$order_id=$_POST['order_id'];
				
				$product_type=$_POST['product_type'];
				$product_weight=$_POST['product_weight'];
				$promotion=$_POST['promotion'];
				$promotion_start=$_POST['promotion_start'];
				$promotion_end=$_POST['promotion_end'];
				$promotion_target=$_POST['promotion_target'];
				$youtube_id=$_POST['youtube_id'];
				$featured=$_POST['featured'];
				$status=$_POST['status'];
				$description_eng=$_POST['description_eng'];
				
				$stock_option_type=$_POST['stock_option_type'];
				$show_stock_option=$_POST['show_stock_option'];
				$show_stock_quantity=$_POST['show_stock_quantity'];
                                
				$remark_eng=$_POST['remark_eng'];
				$actual_price=$_POST['actual_price'];
			
				$show_at_latest=$_POST['show_at_latest'];
				
				$redeemable=$_POST['redeemable'];
				$hide_at_products=$_POST['hide_at_products'];
				$points_required=$_POST['points_required'];
				$clean_url=cleanURL($product_name_eng);
				
				$insert=mysql_query("insert into os_products(cat_id,sub_cat_id,product_name_eng,display_name,clean_url,product_code,product_weight,
				promotion,promotion_start,promotion_end,promotion_target,youtube_id,featured,description_eng,remark_eng,create_datetime,status,actual_price,show_at_latest,product_type,redeemable,hide_at_products,points_required,order_id)
				values($cat_id,$sub_cat,'$product_name_eng','$display_name','$clean_url','$product_code','$product_weight',$promotion,'$promotion_start','$promotion_end','$promotion_target',
				'$youtube_id',$featured,'$description_eng','$remark_eng',now(),$status,'$actual_price','$show_at_latest','$product_type',
				'$redeemable','$hide_at_products','$points_required','$order_id')")or die(mysql_error());
				
				$id=mysql_insert_id();
							
				for($u=1;$u<$totalitem+1;$u++)
				{
					if($_POST['desc'.$u]!="")
					{
						$desc=mysql_real_escape_string($_POST['desc'.$u]);
						$quantity="9999999999";
						$unit_price=$_POST['unit_price'.$u];
						
						$insert_item="insert into os_product_inventory(pid,description,quantity,unit_price)values
									  ($id,'$desc','$quantity','$unit_price')";
						$insert_item_query=mysql_query($insert_item)or die(mysql_error());
					}
				}
				
				if($pic_flag==1)
				{
					$caption = $_POST['caption'];
					$rand=generatePassword(); 
					$newname=$id."_".$rand."_".time().".gif";
					
					$filename = "../productpic/thumbnails/$newname";
					imagejpeg($tmp_s,$filename,75);
					$filename="../productpic/$newname"; 
					imagejpeg($tmp_l,$filename,100);
					
					mysql_query("insert into os_productpic(product_id,order_id,name,caption,width,height,create_datetime)
					   values($id,'0','$newname','$caption',$newlwidth,$newlheight,now())")or die(mysql_error());
					
				}
				header("Location:product_add3.php?id=$id&product_type=$product_type");
			}
		}
	
	
	}
	
	
	
	require("required/header.php");
	
		
?>	
<table width="100%" cellpadding="0" cellspacing="0" height="100%">

    <td valign="top" align="left">
       <table width="95%" align="center">
       		<tr>
            	<td>				
                	 <div class="dw_failed"><?= $msg ?></div>
                     <input type="button" value="Back" onclick="history.go(-1)" />                
                </td>  
      		</tr> 
         </table>
    </td>
</table>					
<?
	require("required/footer.php");
?>               
        